<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
if($_RQ['op']=='get'){
	$setting=uni_setting('pay');
	if(!$setting['credit']){
		exi('系统暂不支持充值！','error');
	}
	$return['credit_activitys']=$setting['credit_activitys']?:array();
	$return['item']=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('credit2'));
	$newcoupons=model('jy_weishop/coupon')->getmycoupon_ing(2,$goodsprice);
	$return['item']['couponid']=$newcoupons[0]['id'];
	$return['coupons']=$newcoupons;
	exi($return);
}
if($_RQ['op']=='recharge'){
	$post=get_postdata();
	if(!intval($post['money'])){
		exi('请先设置充值金额！并且为整数！','error');
	}
	$fee=sprintf("%.2f", intval($post['money']));
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'couponid'=>intval($post['couponid']),
		'fee'=>$fee,
		'createtime'=>TIMESTAMP
	);
	pdo_insert('core_recharge',$data);
	$id=pdo_insertid();
	$params=array(
		'action'=>'recharge',
		'id'=>$id,
		'fee'=>$fee,
		'title'=>'余额充值',
		'plugin'=>'core',
		'pay_type'=>'wechat',
		'no_type'=>array('hdfk','credit'),
		'back_url'=>'core/member.recharge/pay_result',
		'success_url'=>'core/member.log/',
		'error_url'=>'core/member.log/',
	);
	$pay_id=cfc('pay')->create_pay_order($params);
	exi($pay_id);
}
if($_RQ['op']=='pay_result'){
	$recharge=pdo_get('core_recharge',array('id'=>$_RQ['pay_result']['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),array('id','fee','status','couponid'));
	if($recharge['fee']!=$_RQ['pay_result']['fee']){
		exi('支付失败，支付金额不匹配！','error');
	}
	if($recharge['status']){
		exi('该订单已支付！无需重复支付！','error');
	}
	if($_RQ['pay_result']['status']!=1){
		exi('充值失败！','error');
	}
	pdo_update('core_recharge',array('status'=>1,'pay_type'=>$_RQ['pay_result']['pay_type']),array('id'=>$recharge['id'],'uniacid'=>$_SESSION['uniacid']));
	cfc('core')->member_credit($_SESSION['uid'],'credit2',$recharge['fee'],1,'member','会员自助充值');
	$setting=uni_setting('pay');
	if($setting['credit_activitys']){
		//充值赠送
		$give=0;
		foreach($setting['credit_activitys'] as $item){
			if($recharge['fee']>=$item['condition'] && $item['money']>$give){
				$give=$item['money'];
			}
		}
		if($give>0){
			cfc('core')->member_credit($_SESSION['uid'],'credit2',$give,1,'member','会员充值赠送');
		}
	}
	//判断优惠券
	if($recharge['couponid']){
		$couponinfo=pdo_get('jy_weishop_coupon_getlog',array('id'=>$recharge['couponid'],'uniacid'=>$_SESSION['uniacid']));
		$couponinfo['item']=pdo_get('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'id'=>$couponinfo['couponid']),array('backmoney','backcredit','backredpack'));
		if($couponinfo['item']['backmoney']>0){
			cfc('core')->member_credit($_SESSION['uid'],'credit2',$couponinfo['item']['backmoney'],1,'member','充值券赠送');
		}
		if($couponinfo['item']['backcredit']>0){
			cfc('core')->member_credit($_SESSION['uid'],'credit1',$couponinfo['item']['backcredit'],1,'member','充值券赠送');
		}
		if($couponinfo['item']['backredpack']>0){
			$params=array(
				'money'=>$couponinfo['item']['backredpack'],
				'mid'=>$_SESSION['uid'],
				'remark'=>'充值券赠送',
			);
			cfc('red_pack')->get_money($params);
		}
		pdo_update('jy_weishop_coupon_getlog',array('isuse'=>1),array('id'=>$recharge['couponid'],'uniacid'=>$_SESSION['uniacid']));
	}
	$event=array('mid'=>$_SESSION['uid'],'time'=>TIMESTAMP,'fee'=>$recharge['fee']);
	publish('core','member.recharge',$event);
	exi('充值成功！');
}